<template>
  <div class="content">
    <common-head @parking-selected="handleParkingSelected"></common-head>
    <common-search @search="search"></common-search>
    <div class="container">
      <van-loading v-if="loading" class="loading-tip" size="24px">
        数据加载中...
      </van-loading>
      <scroller ref="myscroll" class="scroll-view">
        <div
          v-for="(item, index) in listData"
          :key="index"
          class="order-list"
          :class="[item.status == '1' ? 'occupy' : 'idle']"
          @click.stop="getParkDetail(item)"
        >
          <div class="order-name">泊车号：{{ item.code }}</div>
          <div class="order-status">
            <span :class="[item.status == 0 ? 'idle-text' : 'occupy-text']"
              >状态：{{ item.status == "0" ? "空闲" : "占用" }}</span
            >
            <span :class="[item.status == 0 ? 'idle-text' : 'occupy-text']"
              >设备状态：{{ item.deviceStatus }}</span
            >
          </div>
          <div class="car-num">
            <span>车牌号：{{ item.carNumber }}</span>
            <span v-if="item.vip != 0">{{
              item.vip == 1 ? "白名单用户" : "月卡用户"
            }}</span>
          </div>
        </div>
      </scroller>
      <div class="no-data" v-if="listData.length == 0">
        暂无数据...
      </div>
    </div>

    <van-popup v-model="show" position="bottom" :style="{ height: '75%' }">
      <common-park-detail
        @close="close"
        :berthData="berthData"
      ></common-park-detail>
    </van-popup>
  </div>
</template>

<script>
import commonHead from "../../components/xc/commonHead.vue";
import commonSearch from "../../components/xc/commonSearch.vue";
import commonParkDetail from "../../components/xc/commonParkDetail.vue";
export default {
  components: {
    commonParkDetail,
    commonHead,
    commonSearch
  },
  props: {},
  data() {
    return {
      show: false,
      listData: {},
      loading: true,
      pageNo: 1,
      pageSize: 10,
      flag: 0,
      total: 0,
      berthData: {}
    };
  },
  mounted() {
    if (this.$route.query.cardNumber) {
      let cardNumber = this.$route.query.cardNumber;
      let id = "";
      this.getParkingListByParkId(id, cardNumber);
    } else {
      let cardNumber = "";
      let id = this.$route.query.id;
      this.getParkingListByParkId(id, cardNumber);
    }
  },

  methods: {
    search(val) {
      if (val) {
        this.getParkingListByParkId("", val);
      } else {
        this.getParkingListByParkId("");
      }
    },
    handleParkingSelected(val) {
      this.getParkingListByParkId(val);
    },
    refresh() {
      this.pageNo = 1;
      this.getParkingListByParkId("load");
    },
    infinite() {
      if (this.flag == 0) {
        return;
      }
      if (this.isLoading) {
        this.pageNo += 1;
        this.getParkingListByParkId("load");
      }
    },
    getParkingListByParkId(id, cardNumber) {
      this.baseService
        .get(
          `/parking/parkingCarPort/listForPda?parkingLotsType=20&parkingId=${id}&carNumber=${cardNumber}`,
          {}
        )
        .then(res => {
          let { code, result } = res;
          if (code == 200) {
            this.listData = result;
            this.listData.forEach(element => {
              element.parkId = id;
            });
            this.loading = false;
          }
        })
        .catch(err => {
          this.clearStorage();
          this.$toast("登录失效，请重新登录...");
        })
        .finally(() => {
          this.loading = false; // 确保无论成功失败都关闭加载
        });
    },
    clearStorage() {
      localStorage.removeItem("token");
      localStorage.removeItem("account");
      localStorage.removeItem("password");
    },
    getParkDetail(data) {
      this.berthData = data;
      this.show = true;
    },
    close() {
      this.show = false;
    }
  }
};
</script>

<style lang="less" scoped>
.content {
  height: 100%;
  touch-action: pan-y;
}
.container {
  width: 100%;
  background-image: linear-gradient(to right, #cee7ff, #a5d3ff);
  padding: 10px;
}
.scroll-view {
  top: 2.6rem;
  background-image: linear-gradient(to right, #cee7ff, #a5d3ff);
  padding: 0px 10px;
}
.order-list {
  width: 100%;
  height: 100px;
  background-color: #fff;
  border-radius: 10px;
  margin-top: 10px;
  padding: 10px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-evenly;
  touch-action: pan-y;
  .order-name {
    font-size: 12px;
    font-weight: bold;
  }
  .order-status {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-size: 12px;
      color: #666;
      font-weight: bold;
    }
  }
  .car-num {
    span {
      font-weight: bold;
      font-size: 12px;
      color: #666;
      &:nth-child(2) {
        color: #37af69;
      }
    }
  }
}
.no-data {
  position: relative;
  margin: 10px auto;
  text-align: center;
  color: #3333;
}
.idle {
  background-color: #dbeafe;
}
.occupy {
  background-color: #fee2e2;
}
.order-list .order-status .idle-text {
  color: #37af69;
}
.order-list .order-status .occupy-text {
  color: #fb3c55;
}
</style>
